<template>
	<div class="container">
		<div class="jx-container">
      <div class="jx-table">
        <div class="title">
          <img class="icon" src="/static/img/icon-platform.png"/>
          用户组列表
          <div class="add-new">
            <el-button class="jx-btn" type="primary" @click="addRow">新增用户组</el-button>
          </div>
        </div>
        <el-table :data="list" border style="width: 100%" v-loading="boo.loading">
          <el-table-column type="index" label="序列" width="60" align="center">
            <template slot-scope="scope">
              <span>{{(scope.$index + 1) + ((params_list.page - 1) * params_list.size)}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="adminGroupName" label="用户组" align="center" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column prop="adminAutName" label="权限名称" align="center" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column label="操作" width="180" align="center">
            <template slot-scope="scope">
              <el-button @click="viewRow(scope.row)" type="text" size="small">编辑</el-button>
              <el-button @click="deteleRow(scope.row)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div v-if="list.length != 0" class="jx-pagination">
           <el-pagination @current-change="pageChange" @size-change="handleSizeChange" :current-page="params_list.page" :page-sizes="[10,50,100]" :page-size="params_list.size" layout="total, sizes, prev, pager, next, jumper"
            :total="other.allRow">
          </el-pagination>
        </div>
      </div>
    </div>
    <!-- 新增用户组 -->
    <el-dialog  :visible.sync="boo.showDialog" class="jx-dialog" width="60%">
      <div slot="title" class="header-title">
        <span class="title-name">{{currentRow.adminGroupId ? '编辑用户组' : '新增用户组'}}</span>
      </div>
      <el-form ref="form" :model="currentRow" :rules="rules" label-width="130px" class="jx-form">
        <div class="jx-detail">
          <div class="detail-item">
            <div class="item">
              <div class="title">基本信息</div>
              <el-form-item label="用户组名：" prop="adminGroupName">
                <el-input v-model="currentRow.adminGroupName" placeholder="请输入姓名"></el-input>
              </el-form-item>
              <el-form-item label="选择权限：" prop="adminAutId">
                <el-select class="tel-value" v-model="currentRow.adminAutId" placeholder="请选择">
                  <el-option v-for="(item,index) in typelist" :key="index" :label="item.adminAutName" :value="item.adminAutId"></el-option>
                </el-select>
              </el-form-item>
            </div>
          </div>
        </div> 
      </el-form>
      <div class="btn-container">
        <el-button class="jx-btn" type="warning" @click="boo.showDialog = false">取消</el-button>
        <el-button class="jx-btn" type="primary" :loading="boo.saveing" @click="submitRow('form')">提交</el-button>
      </div>
    </el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
        https: {
          list: this.$api.permit.group,
          authority: this.$api.permit.jurisdiction.page,
        },
        boo: {
          loading: false,
          showDialog: false,
          saveing: false,
        },
        other: {
          allRow: 1,
        },
        list: '',
        currentRow: {},
        params_list: {
          page: 1,
          size: 10,
        },
        typelist: '',
        rules: {
          adminGroupName: [{ required: true, message: '请输入用户组名', trigger: 'blur' }],
          adminAutId: [{ required: true, message: '请选择权限组', trigger: 'change' }],
        },
			}
		},
		mounted(){
      let _self = this
      _self.getList()
      _self.getStatus()
    },
		methods: {
      getStatus(){
        let _self = this
        let success = (res)=>{
          res = res.data
          if(res.retCode == 1){
            _self.typelist = res.retObject
          }else{
            _self.$message.error(res.retMsg)
          }
        }
        _self.$axios.get(_self.https.authority, {}, success) 
      },
      getList(){
        let _self = this
        let params = {
          pageNumber: _self.params_list.page,
          pageSize: _self.params_list.size,
          orderBy: '',
        }
        _self.boo.loading = true
        let success = (res)=>{
          res = res.data
          if(res.retCode == 1){
            _self.list = res.retObject.list
            _self.other.allRow = Number(res.retObject.totalRow)
          }else{
            _self.$message.error(res.retMsg)
          }
          _self.boo.loading = false
        }
        _self.$axios.get(_self.https.list.list, params, success) 
      },
      pageChange(val){
        let _self = this
        _self.params_list.page = val
        _self.getList()
      },
      handleSizeChange(val) {
        let _self = this
        _self.params_list.size = val
        _self.getList()
      },
      addRow(){
        let _self = this
        _self.currentRow = {}
        _self.boo.showDialog = true
      },
      viewRow(item){
        let _self = this
        _self.currentRow = JSON.parse(JSON.stringify(item))
        _self.boo.showDialog = true
      },
      deteleRow(item){
        let _self = this
        let params = {
          id: item.adminGroupId
        }
        let success = (res)=>{
          res = res.data
          if(res.retCode == 1){
            _self.getList()
          }else{
            _self.$message.error(res.retMsg)
          }
        }
        _self.$axios.delete(`${_self.https.list.detail}${params.id}`, params, success) 
      },
      submitRow(formName){
        let _self = this
				this.$refs[formName].validate((vaild)=>{
					if(vaild){  
            _self.boo.saveing = true
            let params = {
              adminGroupName: _self.currentRow.adminGroupName,
              adminAutId: _self.currentRow.adminAutId,
            }    
            let success = (res)=>{
              res = res.data
              if(res.retCode == 1){
                _self.getList()
              }else{
                _self.$message.error(res.retMsg)
              }
              _self.boo.saveing = false
              _self.boo.showDialog = false
            }
            if(_self.currentRow.adminGroupId){
              // params.id = _self.currentRow.adminGroupId
              _self.$axios.put(`${_self.https.list.detail}${_self.currentRow.adminGroupId}`, params, success) 
            }else {
              _self.$axios.post(`${_self.https.list.detail}${params.adminAutId}`, params, success) 
            }
					}else{
						return false
					}
				})
      }
    }
	}

</script>
<style scoped lang="sass">
  .jx-form /deep/ .el-input--small, .jx-form /deep/ .el-textarea
    width: 55%
  .jx-form /deep/ .el-select--small
    width: 100%
  .gray
    font-size: 12px
    margin-left: 5px
</style>
